<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<title>Introduction_to_Algorithms: IntroductionToAlgorithm::DynamicProgrammingAlgorithm Namespace Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Introduction_to_Algorithms
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="namespaces.html"><span>Namespace&#160;List</span></a></li>
      <li><a href="namespacemembers.html"><span>Namespace&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="summary">
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle">
<div class="title">IntroductionToAlgorithm::DynamicProgrammingAlgorithm Namespace Reference</div>  </div>
</div><!--header-->
<div class="contents">

<p>Namespace of <a class="el" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html" title="Namespace of DynamicProgrammingAlgorithm. ">DynamicProgrammingAlgorithm</a>.  
<a href="#details">More...</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a377ef4206d4814024272c15c51051f09"><td class="memTemplParams" colspan="2">template&lt;typename Iterator , typename OutIterator &gt; </td></tr>
<tr class="memitem:a377ef4206d4814024272c15c51051f09"><td class="memTemplItemLeft" align="right" valign="top">std::size_t&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a377ef4206d4814024272c15c51051f09">make_LCS</a> (const Iterator begin, const Iterator end, const std::vector&lt; std::vector&lt; int &gt;&gt; &amp;flag_matrix, typename std::iterator_traits&lt; Iterator &gt;::difference_type seq1_index, typename std::iterator_traits&lt; Iterator &gt;::difference_type seq2_index, OutIterator &amp;out_begin)</td></tr>
<tr class="memdesc:a377ef4206d4814024272c15c51051f09"><td class="mdescLeft">&#160;</td><td class="mdescRight">make_LCS 最长公共子序列的子算法：已知标记矩阵，求最长公共子序列  <a href="#a377ef4206d4814024272c15c51051f09">More...</a><br /></td></tr>
<tr class="separator:a377ef4206d4814024272c15c51051f09"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a02edd40c3c2dd3ce1b514284dd42e99c"><td class="memTemplParams" colspan="2">template&lt;typename Iterator1 , typename Iterator2 , typename OutIterator &gt; </td></tr>
<tr class="memitem:a02edd40c3c2dd3ce1b514284dd42e99c"><td class="memTemplItemLeft" align="right" valign="top">std::size_t&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a02edd40c3c2dd3ce1b514284dd42e99c">longest_common_subsequence</a> (const Iterator1 first_begin, const Iterator1 first_end, const Iterator2 second_begin, const Iterator2 second_end, OutIterator out_begin)</td></tr>
<tr class="memdesc:a02edd40c3c2dd3ce1b514284dd42e99c"><td class="mdescLeft">&#160;</td><td class="mdescRight">longest_common_subsequence 算法导论第15章9.4 最长公共子序列  <a href="#a02edd40c3c2dd3ce1b514284dd42e99c">More...</a><br /></td></tr>
<tr class="separator:a02edd40c3c2dd3ce1b514284dd42e99c"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Namespace of <a class="el" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html" title="Namespace of DynamicProgrammingAlgorithm. ">DynamicProgrammingAlgorithm</a>. </p>
<p>该命名空间内包含所有动态规划算法.动态规划的原理：</p>
<ul>
<li>最优子结构：即原问题的解依赖于子问题的解。</li>
<li>子问题无关：即原问题的解锁依赖的那些子问题是无关的，解耦的。</li>
<li>重叠子问题：即子问题的数量是有限的，即子问题的空间必须足够“小”。原问题的递归算法会反复求解相同的子问题，而不是一直生成新的字问题。 &gt;对比分治法求解的情况：分治法通常在递归的每一步都生成一个全新的子问题。</li>
</ul>
<p>动态规划算法通常利用重叠子问题的性质，对每一个子问题求解一次，将解存入一个表中，每当再次需要求解这个子问题时，直接查表。 </p>
</div><h2 class="groupheader">Function Documentation</h2>
<a class="anchor" id="a02edd40c3c2dd3ce1b514284dd42e99c"></a>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename Iterator1 , typename Iterator2 , typename OutIterator &gt; </div>
      <table class="memname">
        <tr>
          <td class="memname">std::size_t IntroductionToAlgorithm::DynamicProgrammingAlgorithm::longest_common_subsequence </td>
          <td>(</td>
          <td class="paramtype">const Iterator1&#160;</td>
          <td class="paramname"><em>first_begin</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const Iterator1&#160;</td>
          <td class="paramname"><em>first_end</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const Iterator2&#160;</td>
          <td class="paramname"><em>second_begin</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const Iterator2&#160;</td>
          <td class="paramname"><em>second_end</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">OutIterator&#160;</td>
          <td class="paramname"><em>out_begin</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>longest_common_subsequence 算法导论第15章9.4 最长公共子序列 </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">first_begin</td><td>: 第一个序列的起始迭代器 </td></tr>
    <tr><td class="paramname">first_end</td><td>第一个序列的终止迭代器 </td></tr>
    <tr><td class="paramname">second_begin</td><td>: 第二个序列的起始迭代器 </td></tr>
    <tr><td class="paramname">second_end</td><td>第二个序列的终止迭代器 </td></tr>
    <tr><td class="paramname">out_begin</td><td>结果迭代器的起始位置（要求输出容器足够大可以存放结果） </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>最长公共子序列的长度</dd></dl>
<ul>
<li><p class="startli">最长公共子序列算法思想，令 X=&lt; x1,x2,...xm &gt; Y=&lt;y1,y2,...yn&gt; 为两个序列， Z=&lt;z1,z2,...zk&gt;为X和Y的某一个最长公共子序列：</p><ul>
<li>如果 xm=yn，则zk=xm=yn,且Z(k-1)是X(m-1)和Y(n-1)的一个最长公共子序列：</li>
<li>如果 xm != yn, 则如果 zk!=xm,意味着 Z是 X(m-1) 和 Y的一个最长公共子序列</li>
<li>如果 xm != yn, 则如果 zk!=yn,意味着 Z是 Xm 和 Y(n-1)的一个最长公共子序列</li>
</ul>
<p class="startli">因此如果 xm=yn,我们递归求解 X(m-1)和Y(n-1)的最长公共子序列；如果xm != yn，我们求解X(m-1) 和 Y的一个最长公共子序列,以及Xm 和 Y(n-1)的一个最长公共子序列， 这两个之中的最大值即为X和Y的最长公共子序列</p>
</li>
</ul>
<p>定义c[i,j]为Xi和Yj的最长公共子序列长度，则 c[i,j]= 0（若 i=0或j=0) ;c[i-1,j-1]+1 (若i,j&gt;0,且xi=yj)；max(c[i,j-1],c[i-1,j])(若x,j&gt;0 且 xi!=yj)， 通过动态规划方法从底向上计算</p>
<ul>
<li>时间复杂度：O(m*n)，空间复杂度O(m*n) </li>
</ul>

<p>Definition at line <a class="el" href="longest__common__subsequence_8h_source.html#l00113">113</a> of file <a class="el" href="longest__common__subsequence_8h_source.html">longest_common_subsequence.h</a>.</p>

</div>
</div>
<a class="anchor" id="a377ef4206d4814024272c15c51051f09"></a>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename Iterator , typename OutIterator &gt; </div>
      <table class="memname">
        <tr>
          <td class="memname">std::size_t IntroductionToAlgorithm::DynamicProgrammingAlgorithm::make_LCS </td>
          <td>(</td>
          <td class="paramtype">const Iterator&#160;</td>
          <td class="paramname"><em>begin</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const Iterator&#160;</td>
          <td class="paramname"><em>end</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const std::vector&lt; std::vector&lt; int &gt;&gt; &amp;&#160;</td>
          <td class="paramname"><em>flag_matrix</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">typename std::iterator_traits&lt; Iterator &gt;::difference_type&#160;</td>
          <td class="paramname"><em>seq1_index</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">typename std::iterator_traits&lt; Iterator &gt;::difference_type&#160;</td>
          <td class="paramname"><em>seq2_index</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">OutIterator &amp;&#160;</td>
          <td class="paramname"><em>out_begin</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>make_LCS 最长公共子序列的子算法：已知标记矩阵，求最长公共子序列 </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">begin</td><td>: 第一个序列的起始迭代器 </td></tr>
    <tr><td class="paramname">end</td><td>第一个序列的终止迭代器 </td></tr>
    <tr><td class="paramname">flag_matrix</td><td>标记矩阵 </td></tr>
    <tr><td class="paramname">seq1_index</td><td>第一个子序列为X[0..seq1_index1]（从0计数） </td></tr>
    <tr><td class="paramname">seq2_index</td><td>第二个子序列为Y[0..seq1_index2]（从0计数） </td></tr>
    <tr><td class="paramname">out_begin</td><td>存放最长公共子序列结果的起始迭代器（注意必须是引用类型） （要求输出容器足够大可以存放结果） </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>最长公共子序列的长度</dd></dl>
<ul>
<li><p class="startli">最长公共子序列算法思想，令 X=&lt; x1,x2,...xm &gt; Y=&lt;y1,y2,...yn&gt; 为两个序列， Z=&lt;z1,z2,...zk&gt;为X和Y的某一个最长公共子序列：</p><ul>
<li>如果 xm=yn，则zk=xm=yn,且Z(k-1)是X(m-1)和Y(n-1)的一个最长公共子序列：</li>
<li>如果 xm != yn, 则如果 zk!=xm,意味着 Z是 X(m-1) 和 Y的一个最长公共子序列</li>
<li>如果 xm != yn, 则如果 zk!=yn,意味着 Z是 Xm 和 Y(n-1)的一个最长公共子序列</li>
</ul>
<p class="startli">在计算最长公共子序列过程中，顺便计算了标记矩阵。定义c[i,j]为Xi和Yj的最长公共子序列长度，则</p>
</li>
<li>c[i,j]= 0（若 i=0或j=0)</li>
<li>c[i,j]=c[i-1,j-1]+1 (若i,j&gt;0,且xi=yj)</li>
<li>c[i,j]=max(c[i,j-1],c[i-1,j])(若x,j&gt;0 且 xi!=yj)</li>
</ul>
<p>其中：flag_matrix表征的是如何从c[i-1,j-1]、c[i,j-1]、c[i-1,j]这三者之一到达c[i,j]。即flag_matrix（i,j)对应的是矩阵：</p>
<table class="doxtable">
<tr>
<th>c[i][j] c[i+1][j]  </th></tr>
<tr>
<td>c[i+1][j] c[i+1][j+1] </td></tr>
</table>
<ul>
<li>如果 xi=yj，则标记flag_matrix[i-1][j-1]为 11，表示&lt;x1...xi&gt;与&lt;y1...yj&gt;的最长公共子序列也是&lt;x1...x(i-1)&gt;与&lt;y1...y(j-1)&gt;的最长公共子序。此时递归至X(i-1)和Y(j-1)</li>
<li>如果 xi！=yj，且c[i-1,j]&gt; c[i,j-1]则标记flag_matrix[i-1][j-1] 为10，表示&lt;x1...xi&gt;与&lt;y1...yj&gt;的最长公共子序列也是&lt;x1...x(i-1)&gt;与&lt;y1...yj&gt;的最长公共子序。此时递归至X(i-1)和Yj</li>
<li>如果 xi！=yj，且c[i,j-1]&gt; c[i-1,j]则标记flag_matrix[i-1][j-1] 为01，表示&lt;x1...xi&gt;与&lt;y1...yj&gt;的最长公共子序列也是&lt;x1...x&gt;与&lt;y1...y(j-1)&gt;的最长公共子序。此时递归至Xi和Y(j-1)</li>
</ul>
<p>算法时间复杂度O(m+n) ，空间复杂度 O(m*n) </p>

<p>Definition at line <a class="el" href="longest__common__subsequence_8h_source.html#l00062">62</a> of file <a class="el" href="longest__common__subsequence_8h_source.html">longest_common_subsequence.h</a>.</p>

</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></li><li class="navelem"><a class="el" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html">DynamicProgrammingAlgorithm</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.10 </li>
  </ul>
</div>
</body>
</html>
